home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9130 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.9 KB  |  64 lines

  1. Path: bigblue.pvv.unit.no!toriver
  2. From: toriver@bigblue.pvv.unit.no (Tor Iver Wilhelmsen)
  3. Newsgroups: comp.lang.misc,comp.lang.c,comp.lang.pl1
  4. Subject: GOTO controversy
  5. Supersedes: <4hp1vn$rqr@due.unit.no>
  6. Date: 8 Mar 1996 10:29:28 GMT
  7. Organization: or.gan.i.za.tion, n., ... 5. a body of persons organized for some end or work. (Webster)
  8. Message-ID: <4hp268$rs6@due.unit.no>
  9. References: <rcshlds.1.000A6705@mailserv.mta.ca> <4grt4e$8fg@goanna.cs.rmit.EDU.AU> <4hl8mt$4po@newshost.cyberramp.net> <DnwCxp.84C@clw.cs.man.ac.uk>
  10. NNTP-Posting-Host: bigblue.pvv.unit.no
  11. Summary: C backtracking made... well, a little better :-)
  12. Keywords: C,backtracking,goto
  13.  
  14. chl@clw.cs.man.ac.uk (Charles Lindsey) writes:
  15. >
  16. >Well that is what I thougut when I first saw the example, and I was about to
  17. >dash off an article like you have done. But then I looked again and saw
  18. >*exactly* what it was doing.
  19. >
  20. >So if you believe there is a better way, please can we see it?
  21.  
  22. I have a potential solution: The sample below utilizes the "fall-through"
  23. in C's switch-statement to undo what has been done in the reverse order
  24. required.
  25.  
  26. Of course, using break is only _potentially_ nicer than a goto. At least
  27. in this case.
  28.  
  29. - Tor Iver 
  30.  
  31. --- BEGIN included text ---
  32.  
  33. #define once while(0)
  34.  
  35. void main(void)
  36. {
  37.   int btracksteps=0; /* Used to keep a count for backtracking. */
  38.  
  39.   do
  40.   {
  41.     if (!doSomething())
  42.       break;
  43.     if (!doSomethingElse())
  44.       { btracksteps=1; break; }
  45.      /* ... */
  46.   }
  47.   once;
  48.  
  49. switch(btracksteps) {
  50.   case 10: UndoSomethingWayDownInThePrecedingCode();
  51.       /* ... */
  52.   case 2: UndoSomethingElse();
  53.   case 1: UndoSomething();
  54.  } 
  55.  
  56. }
  57.  
  58. --- END included text ---
  59. -- 
  60. Tor Iver Wilhelmsen <toriver@pvv.unit.no> Approx. M. Sc. in CS from NTH, Norway 
  61. Siviling. Tor I. Wilhelmsen // c/o ESD Nordic // Postboks 226 // 1411 Kolbotn
  62. All opinions above invisible unless expressed or put in a blender. Whatever.
  63. Info: http://www.pvv.unit.no/~toriver/  Member of The Software Workshop, NTNU
  64.